.TH std::ranges::all_of,std::ranges::any_of,std::ranges::none_of 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ranges::all_of,std::ranges::any_of,std::ranges::none_of \- std::ranges::all_of,std::ranges::any_of,std::ranges::none_of

.SH Synopsis
   Defined in header <algorithm>
   Call signature
   template< std::input_iterator I, std::sentinel_for<I> S,

             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>>   \fB(1)\fP \fI(since C++20)\fP
   Pred >
   constexpr bool

       all_of( I first, S last, Pred pred, Proj proj = {} );
   template< ranges::input_range R, class Proj = std::identity,

             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>,Proj>> Pred >   \fB(2)\fP \fI(since C++20)\fP
   constexpr bool

       all_of( R&& r, Pred pred, Proj proj = {} );
   template< std::input_iterator I, std::sentinel_for<I> S,

             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>>   \fB(3)\fP \fI(since C++20)\fP
   Pred >
   constexpr bool

       any_of( I first, S last, Pred pred, Proj proj = {} );
   template< ranges::input_range R, class Proj = std::identity,

             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>,Proj>> Pred >   \fB(4)\fP \fI(since C++20)\fP
   constexpr bool

       any_of( R&& r, Pred pred, Proj proj = {} );
   template< std::input_iterator I, std::sentinel_for<I> S,

             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>>   \fB(5)\fP \fI(since C++20)\fP
   Pred >
   constexpr bool

       none_of( I first, S last, Pred pred, Proj proj = {} );
   template< ranges::input_range R, class Proj = std::identity,

             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>,Proj>> Pred >   \fB(6)\fP \fI(since C++20)\fP
   constexpr bool

       none_of( R&& r, Pred pred, Proj proj = {} );

   1) Checks if unary predicate pred returns true for all elements in the range
   [first, last) (after projecting with the projection proj).
   3) Checks if unary predicate pred returns true for at least one element in the range
   [first, last) (after projecting with the projection proj).
   5) Checks if unary predicate pred returns true for no elements in the range
   [first, last) (after projecting with the projection proj).
   2,4,6) Same as (1,3,5), but uses r as the source range, as if using ranges::begin(r)
   as first and ranges::end(r) as last.

   The function-like entities described on this page are niebloids, that is:

     * Explicit template argument lists cannot be specified when calling any of them.
     * None of them are visible to argument-dependent lookup.
     * When any of them are found by normal unqualified lookup as the name to the left
       of the function-call operator, argument-dependent lookup is inhibited.

   In practice, they may be implemented as function objects, or with special compiler
   extensions.

.SH Parameters

   first, last - the range of the elements to examine
   r           - the range of the elements to examine
   pred        - predicate to apply to the projected elements
   proj        - projection to apply to the elements

.SH Return value

   1,2) true if std::invoke(pred, std::invoke(proj, *i)) != false for every iterator i
   in the range, false otherwise. Returns true if the range is empty.
   3,4) true if std::invoke(pred, std::invoke(proj, *i)) != false for at least one
   iterator i in the range, false otherwise. Returns false if the range is empty.
   5,6) true if std::invoke(pred, std::invoke(proj, *i)) == false for every iterator i
   in the range, false otherwise. Returns true if the range is empty.

   See also Notes below.

.SH Complexity

   At most last - first applications of the predicate and the projection.

.SH Possible implementation

                                        all_of (1,2)
  struct all_of_fn
  {
      template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
               std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
      constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const
      {
          return ranges::find_if_not(first, last, std::ref(pred), std::ref(proj)) == last;
      }

      template<ranges::input_range R, class Proj = std::identity,
               std::indirect_unary_predicate<
                   std::projected<ranges::iterator_t<R>,Proj>> Pred>
      constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
      {
          return operator()(ranges::begin(r), ranges::end(r),
                            std::ref(pred), std::ref(proj));
      }
  };

  inline constexpr all_of_fn all_of;
                                        any_of (3,4)
  struct any_of_fn
  {
      template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
               std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
      constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const
      {
          return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last;
      }

      template<ranges::input_range R, class Proj = std::identity,
               std::indirect_unary_predicate<
                   std::projected<ranges::iterator_t<R>,Proj>> Pred>
      constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
      {
          return operator()(ranges::begin(r), ranges::end(r),
                            std::ref(pred), std::ref(proj));
      }
  };

  inline constexpr any_of_fn any_of;
                                       none_of (5,6)
  struct none_of_fn
  {
      template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
               std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
      constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const
      {
          return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) == last;
      }

      template<ranges::input_range R, class Proj = std::identity,
               std::indirect_unary_predicate<
                   std::projected<ranges::iterator_t<R>,Proj>> Pred>
      constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
      {
          return operator()(ranges::begin(r), ranges::end(r),
                            std::ref(pred), std::ref(proj));
      }
  };

  inline constexpr none_of_fn none_of;

.SH Notes

   The return value represented in the form of the Truth table is:

                             input range contains
                all true,  some true, none true, none true,
                none false some false all false  none false
                                                 (empty range)
   1,2) all_of  true       false      false      true
   3,4) any_of  true       true       false      false
   5,6) none_of false      false      true       true

.SH Example


// Run this code

 #include <algorithm>
 #include <functional>
 #include <iostream>
 #include <iterator>
 #include <numeric>
 #include <vector>

 namespace ranges = std::ranges;

 constexpr bool some_of(auto&& r, auto&& pred) // some but not all
 {
     return not (ranges::all_of(r, pred) or ranges::none_of(r, pred));
 }

 constexpr auto w = {1, 2, 3};
 static_assert(!some_of(w, [](int x) { return x < 1; }));
 static_assert( some_of(w, [](int x) { return x < 2; }));
 static_assert(!some_of(w, [](int x) { return x < 4; }));

 int main()
 {
     std::vector<int> v(10, 2);
     std::partial_sum(v.cbegin(), v.cend(), v.begin());
     std::cout << "Among the numbers: ";
     ranges::copy(v, std::ostream_iterator<int>(std::cout, " "));
     std::cout << '\\n';

     if (ranges::all_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; }))
         std::cout << "All numbers are even\\n";

     if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2)))
         std::cout << "None of them are odd\\n";

     auto DivisibleBy = [](int d)
     {
         return [d](int m) { return m % d == 0; };
     };

     if (ranges::any_of(v, DivisibleBy(7)))
         std::cout << "At least one number is divisible by 7\\n";
 }

.SH Output:

 Among the numbers: 2 4 6 8 10 12 14 16 18 20
 All numbers are even
 None of them are odd
 At least one number is divisible by 7

.SH See also

   all_of
   any_of  checks if a predicate is true for all, any or none of the elements in a
   none_of range
   \fI(C++11)\fP \fI(function template)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
